<script setup lang="ts">
  import { ref, reactive } from "vue"
  import { getTenantPage, getTenantTree, getTenantDelete, getTenantrResetPassword } from '@/api/System'
  import { createMessage } from '@/Public-UIlibrary/pb-message/method'
  import tenantModal from './components/tenantModal.vue'
  import dayjs from "dayjs"
  import tenantConfig from './components/tenantConfig.vue'
  let pages = reactive({
    currentPage: 1,
    pageSize: 15,
    total: 0,
  })
  let loading = ref(false)
  const tenantModalRef = ref()
  const tenantConfigRef = ref()
  const leveFormRef = ref()
  let levelListData = reactive([

  ])
  const column = [{

    key: 'tenantCode',
    name: '组织编号',
  },
  {
    key: 'tenantName',
    name: '组织名称',
  },
  {
    key: 'parentTenantName',
    name: '上级组织',

  },
  {
    key: 'adminLoginName',
    name: '管理员账号',
  },
  {
    key: 'contactName',
    name: '联系人',
  },
  {
    key: 'contactPhone',
    name: '电话',
  },
  {
    key: 'createTime',
    name: '创建时间',
  },
  {
    key: 'description',
    name: '备注',
  },
  {
    slot: 'operation',
    name: '操作',
    width: 300,
  }]
  const tenantSearchForm = reactive({
    parentTenantId: '',
    tenantName: '',
    adminLoginName: '',
    beginDate: '',
    endDate: ''
  })
  const tenantDateRang = ref([tenantSearchForm.beginDate, tenantSearchForm.endDate])
  const dateChange = () => {
    tenantSearchForm.beginDate = dayjs(tenantDateRang.value[0]).format('YYYY-MM-DD')
    tenantSearchForm.endDate = dayjs(tenantDateRang.value[1]).format('YYYY-MM-DD')
  }
  let tenantTree = ref([])
  const onInit = () => {
    getTenantPageApi()
    getTenantTreeApi()
  }
  // 确认删除
  const onConfirmDelete = ({ tenantId }) => {
    getTenantDeleteApi(tenantId)
  }
  const onSearch = () => {
    getTenantPageApi()
  }
  // 添加
  const onAddFn = () => {
    tenantModalRef.value.onShow()
  }
  // 编辑 
  const onEdit = (row) => {
    console.log(tenantModalRef);
    tenantModalRef.value.onShow(row)
  }
  // 重置密码
  const onConfirmReset = ({ tenantId }) => {
    getTenantrResetPasswordApi(tenantId)
  }
  // 配置
  const onConfig = (row) => {
    tenantConfigRef.value.onShow(row)
  }
  // 选中树节点
  const treeNodeClick = ({ curNode }) => {
    console.log(curNode);

    tenantSearchForm.parentTenantId = curNode.tenantId
    getTenantPageApi()
  }
  // 分页器改变
  const onPagesChange = () => {
    getTenantPageApi()
  }
  const onReset = () => {
    pages.currentPage = 1
    leveFormRef.value.resetFields()
    getTenantPageApi()
  }
  // 修改或添加成功
  const onConfirmModal = () => {
    onInit()
  }
  // 获取层级接口
  const getTenantPageApi = async () => {
    loading.value = true
    let { total, ...arg } = pages
    let res = await getTenantPage(
      {
        ...arg,
        sortingFields: [{
          field: "",
          order: ""
        }],
        ...tenantSearchForm
      }
    )
    loading.value = false
    if (res.code == 2000) {
      pages.total = res.data.total
      levelListData = [...res.data.records]

    }
  }
  // 获取组织树接口
  const getTenantTreeApi = async () => {
    let res = await getTenantTree({})
    if (res.code == 2000) {
      tenantTree.value = [res.data]
      console.log(tenantTree);

      // console.log(levelList)
    }
  }
  // 重置密码 getTenantrResetPassword
  const getTenantrResetPasswordApi = async (tenantId) => {
    let res = await getTenantrResetPassword({
      tenantId
    })
    if (res.code == 2000) {
      createMessage({
        message: '重置密码成功',
        type: 'success'
      })
      onInit()
    }
  }
  //  删除组织
  const getTenantDeleteApi = async (tenantId) => {
    let res = await getTenantDelete({
      tenantId
    })
    if (res.code == 2000) {
      createMessage({
        message: '删除成功',
        type: 'success'
      })
      onInit()
    }
  }
  onMounted(() => {
    onInit()
  })
</script>

<template>
  <div class="app-container flex flex-col">
    <SearchCard @onSearch="onSearch" @onReset="onReset">
      <el-form :model="tenantSearchForm" ref="leveFormRef" label-width="auto" :inline="true">
        <el-form-item label="创建时间">
          <pb-date-picker v-model="tenantDateRang" placeholder="请输入菜谱日期" @change="dateChange"></pb-date-picker>
        </el-form-item>
        <el-form-item label="组织名称" prop="tenantName">
          <pb-input v-model="tenantSearchForm.tenantName" placeholder="请输入组织名称" />
        </el-form-item>
        <el-form-item label="管理员账号" prop="adminLoginName">
          <pb-input v-model="tenantSearchForm.adminLoginName" placeholder="请输入管理员账号" />
        </el-form-item>
      </el-form>
    </SearchCard>
    <pb-card padding="0px">
      <div v-hasPerm="'center:org:add'">
        <ContentOperation>
          <pb-button size="small" plain @click="onAddFn">+添加</pb-button>
        </ContentOperation>
      </div>

      <div class="flex">
        <ContentLeftTree :treeData="tenantTree" label="tenantName" @nodeClick="treeNodeClick"></ContentLeftTree>
        <pb-card width="84%">

          <pb-table :tableData='levelListData' :loading="loading" :column='column' :bottomElement="['pb-page']">
            <template v-slot:operation="scope">
              <div class="flex">
                <div v-hasPerm="'center:org:config'" v-if="scope.row.systemTag == 'KITCHEN'">
                  <pb-button text @click="onConfig(scope.row)">配置</pb-button>
                  <p class="divider mr-2 ml-2"></p>
                </div>
                <div v-hasPerm="'center:org:reset'">
                  <PopconfirmButton text=" 重置密码"
                    :content="`请确认是否重置${scope.row.adminLoginName} 账号密码,重置后初始密码为：hao123456 `"
                    @confirm="onConfirmReset(scope.row)">
                  </PopconfirmButton>
                  <p class="divider mr-2 ml-2"></p>
                </div>
                <div v-hasPerm="'center:org:edit'">
                  <pb-button text @click="onEdit(scope.row)">编辑</pb-button>
                  <p class="divider mr-2 ml-2"></p>
                </div>
                <div v-hasPerm="'center:org:delete'">
                  <PopconfirmButton @confirm="onConfirmDelete(scope.row)"></PopconfirmButton>
                </div>

              </div>
            </template>
          </pb-table>
          <pb-pagination v-model:currentPage="pages.currentPage" v-model:pageSize="pages.pageSize"
            @onChange="onPagesChange" :total="pages.total"></pb-pagination>
        </pb-card>
      </div>
    </pb-card>

    <tenantModal ref="tenantModalRef" @onConfirmModal="onConfirmModal" :tenantTreeData="tenantTree"></tenantModal>
    <tenantConfig ref="tenantConfigRef" @onConfirmModal="onConfirmModal" :tenantTreeData="tenantTree"></tenantConfig>
  </div>
</template>

<style lang="less" scoped></style>